package com.itheima.reggie.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.mapper.DishFlavorMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: Rodgers
 * @Date 2022/3/11 14:57
 */
@Service
public class DishServiceImpl implements DishService {

    @Autowired(required = false)
    private DishMapper dishMapper;

    @Autowired(required = false)
    private DishFlavorMapper dishFlavorMapper;

    /**
     * 添加菜品
     *
     * @param dishDto
     * @return
     */
    @Transactional //springboot默认已经开启了事务注解扫描，所以这里直接使用就可以了
    @Override
    public boolean save(DishDto dishDto, HttpSession session) {
        //1.完善菜品对象数据

        //获取登录人id
        Long id = (Long) session.getAttribute("employee");

        //完善修改人，创建人
        dishDto.setUpdateUser(id);
        dishDto.setCreateUser(id);
        //(修改时间，添加时间，sort默认0，是否禁销设置1起售)
        dishDto.setUpdateTime(LocalDateTime.now());
        dishDto.setCreateTime(LocalDateTime.now());
        dishDto.setSort(0);
        dishDto.setStatus(1);

        //2.调用数据访问层添加菜品，要求获取到添加后菜品主键id
        dishMapper.insert(dishDto);  //插入后面就有菜品id

        //3.遍历口味记录列表完善数据
        List<DishFlavor> dishFlavorList = dishDto.getFlavors().stream().map((dishFlavor -> {
            //dishFlavor是获取集合中的每个元素对象
            // (设置菜品的主键、更新时间、创建时间、更新人、创建人)
            dishFlavor.setDishId(dishDto.getId());
            dishFlavor.setUpdateTime(LocalDateTime.now());
            dishFlavor.setCreateTime(LocalDateTime.now());
            dishFlavor.setUpdateUser(dishDto.getUpdateUser());
            dishFlavor.setCreateUser(dishDto.getCreateUser());
            return dishFlavor;
        })).collect(Collectors.toList());

        //4.调用数据访问层插入多条口味记录数据
        dishFlavorMapper.save(dishFlavorList);
        return true;
    }

    /**
     * 分页查询(连表查)
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public Page<DishDto> findByPage(Integer page, Integer pageSize, String name) {
        //注意：对哪一条sql语句生成分页sql语句，这三句必须紧挨着
        //设置每页的起始序号
        PageHelper.startPage(page,pageSize);
        //获得每页的信息集合
        List<DishDto> dishDtoList = dishMapper.findAll(name);
        //将分页集合交给pageInfo专门帮我们分页
        PageInfo<DishDto> pageInfo = new PageInfo<>(dishDtoList);
        //封装分页信息
        Page<DishDto> dtoPage = new Page<>();
        dtoPage.setRecords(pageInfo.getList());
        dtoPage.setTotal(pageInfo.getTotal());

        return dtoPage;
    }

    /**
     * 根据id查询指定菜品和多个口味
     *
     * @param id
     * @return
     */
    @Override
    public DishDto findById(Long id) {
        return dishMapper.findById(id);
    }

    /**
     * 套餐的菜品回显
     *
     * @param categoryId
     * @param status
     * @return
     */
    @Override
    public List<DishDto> findByCategoryId(Long categoryId, Integer status) {
        //1.调用mapper层根据类别id和状态查询菜品列表
        List<Dish> dishList = dishMapper.findByCategoryIdAndStatus(categoryId, status);
        //2.加工出来dishList封装到List<DishDto>，完善里面口味列表数据
        //dishDto = (dishList)菜品列表 + (dishFlavorList)口味列表
        List<DishDto> dishDtoList = dishList.stream().map(dish -> {
            //实例DishDto对象封装数据
            DishDto dishDto = new DishDto();

            //将dish对象所有数据赋值给dishDto对象
            BeanUtils.copyProperties(dish,dishDto);
            //根据菜品id获取口味列表数据封装到dishDto里面
            List<DishFlavor> dishFlavorList = dishFlavorMapper.findByDishId(dishDto.getId());
            dishDto.setFlavors(dishFlavorList);

            return dishDto;
        }).collect(Collectors.toList());


        return dishDtoList;
    }
}
